package com.liqing.movecomment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.github.pagehelper.PageInfo;
import com.liqing.movecomment.annotation.ResponseResult;
import com.liqing.movecomment.dao.CommentDao;
import com.liqing.movecomment.entity.Comment;
import com.liqing.movecomment.entity.User;
import com.liqing.movecomment.enums.Constent;
import com.liqing.movecomment.enums.RedisConstant;
import com.liqing.movecomment.service.CommentService;
import com.liqing.movecomment.util.RedisUtils;

import com.liqing.movecomment.util.SensitiveWordFilterUtils;

import com.liqing.movecomment.util.ThreadLocalUtils;
import com.liqing.movecomment.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Slf4j

@ResponseResult
@Api(tags = "评论服务√")
@RequestMapping("comment")
public class CommentController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;



    @PostMapping("/sava")
    @ApiOperation("发布评论")
    public MessageVo save(@RequestBody @Validated CommentPostVo commentPostVo) {
        if (ObjectUtils.isNull(commentPostVo.getMessage())) {
            throw new RuntimeException("message不能为空");
        }


        SensitiveWordFilterUtils sw = new SensitiveWordFilterUtils();
        sw.InitializationWork()
        ;
        log.info("检测字的长度：{}",commentPostVo.getMessage().length());

       String str = sw.filterInfo(commentPostVo.getMessage());
       log.info("过滤之后的文字:{}",str);
        commentPostVo.setMessage(str);

        return commentService.save(commentPostVo);


    }


    @GetMapping("/listAllComments")
    @ApiOperation("电影id 拿到所有的评论")
    public Map listAllComments(
            @RequestParam("otherId") Long otherId,
            @RequestParam("size") int size,
            @RequestParam("current") int current
    ) {
        PageInfo<CommentRootVo> list = commentService.listAllComments(otherId, size, current);
        final Map map = new HashMap<>();
        map.put("page", list);
        Long commentCount = 0L;
        map.put("commentCount", commentCount);
        return map;
    }



    /*
     * 点赞 评论
     * */

    @ApiOperation("点赞评论")
    @GetMapping("/likeComment")
    public Long likeComment(Integer commentId) {
        String key = RedisConstant.move.COMMENT.LIKE_SET + commentId;
        User user = (User) ThreadLocalUtils.get();
         RedisUtils.addSetKey(key, String.valueOf(user.getId()));
     return    RedisUtils.getSetCount(key);
    }

    @ApiOperation("取消点赞评论")
    @GetMapping("/unLikeComment")
    public Long unLikeComment(Integer commentId) {
        String key = RedisConstant.move.COMMENT.LIKE_SET + commentId;
        User user = (User) ThreadLocalUtils.get();
        RedisUtils.removeSetKey(key, String.valueOf(user.getId()));
        return RedisUtils.getSetCount(key);
    }


    /**
     * 删除数据
     *
     * @param commentId
     * @return 删除是否成功
     */
    @ApiOperation("删除评论")
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer commentId, Long otherId) {

        return ResponseEntity.ok(this.commentService.deleteById(commentId));
    }

    /**
     * 拿到评论我的评论
     * @param page
     * @param size
     * @return
     */
    @GetMapping("AnswerMe")
    public PageInfo<AnswerMeVo> getMyComment(Integer page,Integer size){
        return commentService.anwserMe(page,size);
    }

    public Boolean deleteById(Integer id){
        return commentService.deleteById(id);
    }



}

